package java8;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 并行的，只能消费一次
 * @auther hanchao
 * @date 2017/3/7 15:02
 */
public class StreamTest {
    public static void main(String[] args){
        List<String> list = new ArrayList<>();
        list.add("asdf");
        list.add("zxc");
        list.add("dvczzxcv");
        list.add("xcvdf");


        System.out.println(list.stream().findFirst());
        System.out.println(list.stream().anyMatch((x) -> x.length() < 2));

        System.out.println(list.stream().map((x) -> x.length()).toArray()[2]);
        //查看所有的长度
        //这个静态应用就是引用了 方法体，然后对应到operator上了，，，8想融会贯通好难
        System.out.println(list.stream().map((x) -> x.length()).reduce(0,Integer::sum));
        //规约找最小值
        System.out.println(list.stream().map((x) -> x.length()).reduce(Integer::min));

        System.out.println("查找最长的字符串--------------->");
        System.out.println(list.stream().reduce((x,y) -> {if(x.length() > y.length()){return x;}return y;}));

        System.out.println(list.stream().map((x) -> x.length()).collect(Collectors.toList()));

        System.out.println("查看内部遍历的过程");


        List t = list.stream()
                .filter((x) -> {System.out.println(">>>>"+x);return x!=null;})
                .map((x) -> {System.out.println("》》"+x);return x.length();})
                .limit(2)//top...,配合skip,limit可以分页
                .distinct()//去重
                .collect(Collectors.toList());
        //limit有一种短路的技巧（多线程怎么做到的通知？）
        System.out.println(t);
        //三件事，数据源，中间操作链，终端操作（返回一个非流的值）
        //anyMatch终端操作
        System.out.println("==============");

        String [] arrayWords = {"hello","world"};
        Stream<String> streamOfwords = Arrays.stream(arrayWords);
        /*List tmp = streamOfwords.map(word -> word.split(""))
                .map(Arrays::stream)//这一步肯定不行
                .distinct()
                .collect(Collectors.toList());*/
        List tmp = streamOfwords.map(word -> word.split(""))
                .flatMap(Arrays::stream)//这一步肯定不行
                .distinct()
                .collect(Collectors.toList());
        System.out.println(tmp);


        //自定义收集器可能可以提升性能
    }
}
